//
//  ZCComposeToolBar.swift
//  weibo
//
//  Created by Ning Xie on 16/8/16.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

enum ZCComposeToolBarButtonType: Int {
    case Picture = 0
    case Mention = 1
    case Trend = 2
    case Emotion = 3
    case Add = 4
}

class ZCComposeToolBar: UIStackView {

    var didSelectedButtonClosure:((type:ZCComposeToolBarButtonType)->())?
    
    var emoticonButton:UIButton = UIButton()
    var addButton:UIButton = UIButton()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
        axis = .Horizontal
        distribution = .FillEqually
        
        
        addChildButton("compose_toolbar_picture",type: .Picture)
        addChildButton("compose_mentionbutton_background",type: .Mention)
        addChildButton("compose_trendbutton_background",type: .Trend)
        emoticonButton = addChildButton("compose_emoticonbutton_background",type: .Emotion)
        addButton = addChildButton("compose_add_background",type: .Add)
        
    }
    
    private func addChildButton(imageName:String,type:ZCComposeToolBarButtonType)->UIButton{
        
        let button = UIButton(type: UIButtonType.Custom)
        
        button.tag = type.rawValue
        button.adjustsImageWhenHighlighted = false
        button.setImage(UIImage(named: imageName), forState: .Normal)
        button.setImage(UIImage(named: "\(imageName)_highlighted"), forState: .Highlighted)
        button.setBackgroundImage(UIImage(named: "compose_toolbar_background"), forState: .Normal)
        button.addTarget(self, action: #selector(ZCComposeToolBar.buttonAction(_:)), forControlEvents: .TouchUpInside)
        
        addArrangedSubview(button)
        
        return button
    }
    
    @objc private func buttonAction(button:UIButton){
        
        let type = ZCComposeToolBarButtonType(rawValue: button.tag)
        
        didSelectedButtonClosure?(type: type!)
        
    }
    
    func switchEmoticonButtonImage(isEmoticon:Bool){
        if isEmoticon {
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background"), forState: .Normal)
            emoticonButton.setImage(UIImage(named: "compose_keyboardbutton_background_highlighted"), forState: .Highlighted)
        }else{
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background"), forState: .Normal)
            emoticonButton.setImage(UIImage(named: "compose_emoticonbutton_background_highlighted"), forState: .Highlighted)
        }
    }
}
